package LawsonTransformJava.pgm;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;

import LawsonTransformJava.dao.DAOFactoryAccessDB;
import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAOHRUserFlds;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblHRUserFlds;


public class HR515HRUserFlds extends DAObject implements DAOHRUserFlds {

	public HR515HRUserFlds() {
	    // initialization 
			try {
				// There is no separate output file for PAEmployees.
				// It is assigned to an appropriate Employee record.
				this.output = null;
				init();
			} catch (ClassNotFoundException e) {
			    // Could not find the database driver
				System.out.println("ClassNotFoundException: " + e);
			} catch (SQLException e) {
			    // Could not connect to the database
				System.out.println("SQLException: " + e);
			} catch (IOException e) {
				// Could no write properties file
				System.out.println("IOException: " + e);
			}
	}
	
	@Override
	public TblHRUserFlds findHRUserFlds(String field_key) {
		try {
//			whereClause = " WHERE FIELD_KEY=" + field_key;
			int temp_key = 83;
			whereClause = " WHERE FIELD_KEY=" + temp_key;
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause);
			rslt = stmtData.executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			rslt.next();
			return createHRUserFlds(rslt);
			//createFields(emp);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return new TblHRUserFlds();
	}

	public boolean hasMore () {
	  return hasMoreRecs;
	}

		@Override
	public int countHRUserFlds() {
		try {
			countRecs = stmtData.executeQuery("SELECT COUNT(*) FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause));
			countRecs.next();
			return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
	}

	@Override
	public ResultSet selectHRUserFldsRS(String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection selectHRUserFldsTO(String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	 private TblHRUserFlds createHRUserFlds(ResultSet rslt) throws SQLException, InvocationTargetException, IllegalAccessException,
		IllegalArgumentException {
		TblHRUserFlds hru = new TblHRUserFlds();
		Method[] mthds = hru.getClass().getMethods();
		int i=0;
		for(i=0;i<rsmd.getColumnCount();i++) {
			if (rsmd.getColumnTypeName(i+1)=="CHAR") {
				int j=0;
				for(j=0;j<mthds.length;j++) {
					if (mthds[j].getName().equalsIgnoreCase("set" + rsmd.getColumnName(i+1)))
					{
						mthds[j].invoke(hru, "\"" + rslt.getString(i+1).trim() + "\"");
						//Get out of loop
						j=mthds.length+1;
					}
				}
			}
			else if(rsmd.getColumnTypeName(i+1)=="NUMBER")
				if(rsmd.getScale(i+1)==0) {
					for(int j=0;j<mthds.length;j++) {
						if (mthds[j].getName().equalsIgnoreCase("set" + rsmd.getColumnName(i+1)))
						{
							mthds[j].invoke(hru, rslt.getInt(i+1));
							//Get out of loop
							j=mthds.length+1;
						}
					}
				}
				else
					for(int j=0;j<mthds.length;j++) {
						if (mthds[j].getName().equalsIgnoreCase("set" + rsmd.getColumnName(i+1)))
						{
							mthds[j].invoke(hru, rslt.getFloat(i+1));
							//Get out of loop
							j=mthds.length+1;
						}
					}
		}
		return hru;
	 }

	  private void init() throws SQLException, ClassNotFoundException, IOException {
		    // Set the data and xref variables
		    data = DAOFactoryOracleDB.getDAOOracleDB(true);
		    xref = new DAOFactoryAccessDB();
		    sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
		    stmtData = data.getDataStatement();	    
		    countStmtData = data.getCountStatement();
		    //Set the Date Format for output
		    df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	  }
}